package com.lin.missyou.dto.validators;

import com.lin.missyou.dto.TokenGetDTO;
import org.springframework.util.StringUtils;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

//第一个泛型是自定义注解
//第二个泛型是自定义注解修饰的目标的类型
public class TokenPasswordValidator implements ConstraintValidator<TokenPassword, String> {
    private int min;
    private int max;

    //获取注解的相关信息、方法
    @Override
    public void initialize(TokenPassword constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        //小程序不需要密码，密码置空
        if (StringUtils.isEmpty(s)) {
            return true;
        }
        return min <= s.length() && s.length() <= max;
    }


}
